package com.yanguilai.rabbitmqtest.send;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Component
public class Send {

    private final static String QUEUE_NAME = "hello";

    private static ConnectionFactory factory;

    static {
        factory = new ConnectionFactory();
        factory.setHost("c7formal");
    }

    public void send() {
        try {
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String msg = "Hello, world!";
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
            System.out.println(" [x] Sent '" + msg + "'");
        } catch (TimeoutException | IOException e) {
            e.printStackTrace();
        }
    }
}
